<!DOCTYPE html>
<html lang="zh_CN" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<head>
    <!--公用头部-->
    <th:block th:replace="common/head::commonHead('定时任务-小邑')" />
    <style>
    </style>
</head>
<!--公用js-->
<th:block th:replace="common/head::commonJs"/>
<body marginwidth="0" marginheight="0">
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <form class="layui-form" id="scheduleForm">
                <div class="layui-form">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">任务名称</label>
                            <div class="layui-input-inline">
                                <input id="jobName" name="jobName" class="layui-input" autocomplete="off"
                                       type="text" placeholder="请输入任务名称"/>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="button" id="searchBtn"
                                    class="xy-btn xy-btn-primary icon-btn xy-btn-sm">
                                <i class="layui-icon">&#xe615;</i> 搜索
                            </button>
                        </div>
                    </div>
                </div>
            </form>
        </div>
    </div>
    <div class="layui-card">
        <div class="layui-card-body">
            <table class="layui-hide" id="jobTable" lay-filter="jobTable"></table>
            <script id="jobToolbar" type="text/html">
                <button sec:authorize="hasPermission('/scheduleJob/addScheduleJob','scheduleJob:addScheduleJob')"type="button" class="xy-btn xy-btn-primary xy-btn-sm" lay-event="add">
                    <i class="layui-icon">&#xe654;</i> 新增
                </button>
                <button sec:authorize="hasPermission('/scheduleJob/deleteBatch','scheduleJob:deleteBatch')" type="button" class="xy-btn xy-btn-sm xy-btn-danger" lay-event="delete">
                    <i class="layui-icon">&#xe640;</i> 删除
                </button>
                <button class="xy-btn xy-btn-sm"  lay-event="runLog">
                    <i class="layui-icon layui-icon-file"></i> 日志
                </button>
            </script>
            <script id="jobBar" type="text/html">
                <button sec:authorize="hasPermission('/scheduleJob/updateScheduleJob','scheduleJob:updateScheduleJob')"
                        class="xy-btn xy-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit">修改</i>
                </button>
                {{#if (d.status == 0) { }}
                <button sec:authorize="hasPermission('/scheduleJob/stopScheduleJob','scheduleJob:stopScheduleJob')"
                        class="xy-btn xy-btn-xs xy-btn-warming" plain lay-event="pause"><i class="layui-icon layui-icon-pause">停止</i>
                </button>
                {{# }else if(d.status == 1){ }}
                <button sec:authorize="hasPermission('/scheduleJob/resetScheduleJob','scheduleJob:resetScheduleJob')"
                        class="xy-btn xy-btn-xs xy-btn-warming" plain lay-event="resume"><i class="layui-icon layui-icon-play">恢复</i>
                </button>
                {{# } }}
                <button sec:authorize="hasPermission('/scheduleJob/submitScheduleJob','scheduleJob:submitScheduleJob')"
                        class="xy-btn xy-btn-xs xy-btn-success" plain lay-event="run"><i class="layui-icon layui-icon-release">执行</i>
                </button>
                <button sec:authorize="hasPermission('/scheduleJob/delete','scheduleJob:delete')"
                        class="xy-btn xy-btn-xs xy-btn-danger" plain lay-event="remove"><i class="layui-icon layui-icon-delete">删除</i>
                </button>
            </script>
            <script id="jobStatus" type="text/html">
                {{#if (d.status == 0) { }}
                <span>运行中</span>
                {{# }else if(d.status == 1){ }}
                <span>已挂起</span>
                {{# } }}
            </script>
        </div>
    </div>
</div>
</body>

<script>
    layui.use(['table', 'layer', 'jquery', 'util', 'form', 'notice'], function () {
        var layer = layui.layer
            , table = layui.table
            , form = layui.form
            , notice = layui.notice
            , $ = layui.jquery
            , util = layui.util;
        util.fixbar({
            top: true //返回顶部
            , css: { right: 50, bottom: 50 }
            , bgcolor: '#3b7cd8'
            , click: function (type) {
            }
        });
        var scheduleTable =  table.render({
            elem: '#jobTable'
            ,method:'post'
            ,url: basePath + '/scheduleJob/list' //数据接口
            ,page: true //开启分页
            ,toolbar: '#jobToolbar' //开启工具栏，此处显示默认图标，可以自定义模板，详见文档
            ,cellMinWidth: 100
            ,cols: [ [ //表头
                {type: 'checkbox'},
                {title: '名称', field: 'jobName', align: 'center', width: 100},
                {title: '运行类', field: 'beanName', align: 'center'},
                {title: '参数', field: 'params', align: 'center'},
                {title: '表达式', field: 'cronExpression', align: 'center'},
                {title: '状态', field: 'status', align: 'center', templet: '#jobStatus'},
                {title: '描述', field: 'remark', align: 'center'},
                {title: '操作', toolbar: '#jobBar', align: 'center', width: 300}
            ] ]
            ,limit: 10
            ,limits: [10, 20, 50, 100]
            ,page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
                layout: [  'prev', 'page', 'next', 'skip','count','limit'] //自定义分页布局
                , curr: 1 //设定初始在第 5 页
                , groups: 5 //只显示 1 个连续页码
                , first: false //不显示首页
                , last: false //不显示尾页
                , theme:"#1E9FFF"
            }
        });

        table.on('tool(jobTable)', function (obj) {
            if (obj.event === 'remove') {
                window.remove(obj);
            } else if (obj.event === 'edit') {
                window.edit(obj);
            } else if (obj.event === 'pause') {
                window.pause(obj);
            } else if (obj.event === 'resume') {
                window.resume(obj);
            } else if (obj.event === 'run') {
                window.run(obj);
            }
        });

        table.on('toolbar(jobTable)', function (obj) {
            if (obj.event === 'add') {
                window.add();
            } else if (obj.event === 'delete') {
                window.batchRemove(obj);
            } else if (obj.event === 'runLog') {
                window.runLog();
            }
        });

        // 搜索
        $('#searchBtn').on('click',function () {
            scheduleTable.reload({
                where: getFormData('scheduleForm')
            });
        });
        // 添加
        window.add = function () {
            layer.open({
                type: 2,
                title: '添加定时任务',
                skin: 'layer-ext-myskin',
                shadeClose: false,
                shade: 0.3,
                area: ['1000px', '445px'],
                content:  basePath+ '/view/system/schedule/scheduleJob_add_update?type=1'
            });
        }
        // 修改
        window.edit = function (obj) {
            layer.open({
                type: 2,
                title: '修改定时任务',
                skin: 'layer-ext-myskin',
                shadeClose: false,
                shade: 0.3,
                area: ['1000px', '445px'],
                content:  basePath+ '/view/system/schedule/scheduleJob_add_update?type=2&jobId=' + obj.data['jobId']
            });
        }
        // 运行日志
        window.runLog = function () {
            layer.open({
                type: 2,
                title: '运行日志',
                skin: 'layer-ext-myskin',
                shadeClose: false,
                shade: 0.3,
                area: ['90%', '90%'],
                content: basePath+ '/view/system/schedule/findScheduleLog'
            });
        }
        // 停止定时任务
        window.pause = function (obj) {
            var loadIndex;
            layer.confirm('确定要停止定时任务吗？',{icon:5, title:'提示',skin: 'layer-ext-myskin'}, function(index) {
                layer.close(index);
                sendAjaxPostToCallbackJson(
                    basePath + '/scheduleJob/stopScheduleJob',
                    {
                        jobId:obj.data['jobId']
                    },
                    function (result){
                        if (result.success){
                            // 提示信息
                            top.layui.notice.msg(result.msg ,{icon: 1,position:"topCenter"});
                            table.reload('jobTable',{
                                page:{curr:1}
                            });
                        }else{
                            top.layui.notice.msg(result.msg ,{icon: 2,position:"topCenter"});
                        }
                    },
                    function (data) {
                        if (utils.string.isEmpty(data) && utils.string.isEmpty(data.msg)){
                            top.layui.notice.msg(data.msg ,{icon: 2,position:"topCenter"});
                        }else{
                            top.layui.notice.msg("服务器内部错误，请联系管理员~", {icon: 2,position:"topCenter"});
                        }
                    },
                    function (){
                        loadIndex = layer.load(1);
                    },
                    function (){
                        layer.close(loadIndex);
                    }
                )
            });
        }
        // 恢复定时任务
        window.resume = function (obj) {
            var loadIndex;
            layer.confirm('确定要恢复定时任务吗？',{icon:5, title:'提示',skin: 'layer-ext-myskin'}, function(index) {
                layer.close(index);
                sendAjaxPostToCallbackJson(
                    basePath + '/scheduleJob/resetScheduleJob',
                    {
                        jobId:obj.data['jobId']
                    },
                    function (result){
                        if (result.success){
                            // 提示信息
                            top.layui.notice.msg(result.msg ,{icon: 1,position:"topCenter"});
                            table.reload('jobTable',{
                                page:{curr:1}
                            });
                        }else{
                            top.layui.notice.msg(result.msg ,{icon: 2,position:"topCenter"});
                        }
                    },
                    function (data) {
                        if (utils.string.isEmpty(data) && utils.string.isEmpty(data.msg)){
                            top.layui.notice.msg(data.msg ,{icon: 2,position:"topCenter"});
                        }else{
                            top.layui.notice.msg("服务器内部错误，请联系管理员~", {icon: 2,position:"topCenter"});
                        }
                    },
                    function (){
                        loadIndex = layer.load(1);
                    },
                    function (){
                        layer.close(loadIndex);
                    }
                )
            });
        }
        // 执行定时任务
        window.run = function (obj) {
            var loadIndex;
            layer.confirm('确定要执行定时任务吗？',{icon:5, title:'提示',skin: 'layer-ext-myskin'}, function(index) {
                layer.close(index);
                sendAjaxPostToCallbackJson(
                basePath + '/scheduleJob/runScheduleJob',
                {
                    jobId:obj.data['jobId']
                },
                function (result){
                    if (result.success){
                        // 提示信息
                        top.layui.notice.msg(result.msg ,{icon: 1,position:"topCenter"});
                        table.reload('jobTable',{
                            page:{curr:1}
                        });
                    }else{
                        top.layui.notice.msg(result.msg ,{icon: 2,position:"topCenter"});
                    }
                },
                function (data) {
                    if (utils.string.isEmpty(data) && utils.string.isEmpty(data.msg)){
                        top.layui.notice.msg(data.msg ,{icon: 2,position:"topCenter"});
                    }else{
                        top.layui.notice.msg("服务器内部错误，请联系管理员~", {icon: 2,position:"topCenter"});
                    }
                },
                function (){
                    loadIndex = layer.load(1);
                },
                function (){
                    layer.close(loadIndex);
                }
                )
            });
        }
        // 单记录删除
        window.remove = function (obj) {
            // 单记录删除
            layer.confirm('确定要删除吗？',{icon:5, title:'提示',skin: 'layer-ext-myskin'},function(index) {
                layer.close(index);
                sendAjaxPostToError(
                    basePath+ '/scheduleJob/delete',
                    {id : obj.id},
                    function (result){
                        if (result.success){
                            top.layui.notice.msg(result.msg ,{icon: 1,position:"topCenter"});
                            table.reload('jobTable',{
                                page:{curr:1}
                            });
                        }else {
                            top.layui.notice.msg(result.msg ,{icon: 2,position:"topCenter"});
                        }
                    },
                    function (data) {
                        if (utils.string.isEmpty(data) && utils.string.isEmpty(data.msg)){
                            top.layui.notice.msg(data.msg ,{icon: 2,position:"topCenter"});
                        }else{
                            top.layui.notice.msg("服务器内部错误，请联系管理员~", {icon: 2,position:"topCenter"});
                        }
                    }
                )
            });
        }
        // 批量删除
        window.batchRemove = function (obj) {
            if (obj.length === 0) {
                top.layui.notice.msg("未选择数据~" ,{icon: 3,position:"topCenter"});
            } else {
                layer.confirm('确定要删除选中数据吗？',{icon:5, title:'提示',skin: 'layer-ext-myskin'}, function(index) {
                    var ids = [];
                    $.each(obj, function(index,element) {
                        ids.push(element.id);
                    });
                    layer.close(index);
                    // 异步请求
                    sendAjaxPostToError(
                        basePath+ '/scheduleJob/deleteBatch',
                        {ids: ids},
                        function (result){
                            if (result.success){
                                top.layui.notice.msg(result.msg ,{icon: 1,position:"topCenter"});
                                table.reload('jobTable',{
                                    page:{curr:1}
                                });
                            }else {
                                top.layui.notice.msg(result.msg ,{icon: 2,position:"topCenter"});
                            }
                        },
                        function (data) {
                            if (utils.string.isEmpty(data) && utils.string.isEmpty(data.msg)){
                                top.layui.notice.msg(data.msg ,{icon: 2,position:"topCenter"});
                            }else{
                                top.layui.notice.msg("服务器内部错误，请联系管理员~", {icon: 2,position:"topCenter"});
                            }
                        }
                    )
                });
            }
        }
    })
</script>
</html>